
<blockquote class="layui-elem-quote">分类列表</blockquote>

<table id="category-list" lay-filter="category-list"></table>

<!-- 行工具条 -->
<script type="text/html" id="category-bar">
    <a class = "layui-btn layui-btn-sm layui-btn-normal" lay-event="edit">编辑</a>
    <a class = "layui-btn layui-btn-sm layui-btn-danger" lay-event="del">删除</a>
</script>
<script>
    layui.use(['table'],function (){
       var table = layui.table;
       $ = layui.$;
       //ajax获取数据，渲染到表格中
        table.render({
            elem: '#category-list',
            url:'/api/category',    //获取分类信息
            page: true,     //开启分页
            cols: [ [
                //每个列如何渲染
                {field: 'id',title: 'ID',},
                {field: 'name',title: 'sp名',},
                {field: 'dscp',title: '描述',},
                {field: 'pic',title: '图片',templet: function (d){
                    if(d.pic){
                        return '<img src="/upload/'+d.pic+'">';
                    }else{
                        return "";
                    }
                    }},
                {field: 'parentId',title: '父分类',templet: function (d){
                    return (d.parent)? d.parent.name : '';
                }},
                {field: 'recom',title: '主页推荐', templet: function (d){
                        return ['不推荐','推荐'][d.recom];
                },},
                {field: 'status',title: '状态',templet: function (d){
                        return ['上架','下架'][d.status];
                }},
                {title: '操作',toolbar: '#category-bar'},
            ] ],
            //数据的解析
            parseData: function (d){
                return {
                    code: d.code,
                    msg: d.msg,
                    count: d.data.total,
                    data: d.data.list,
                };
            },
        });

        //行工具条的点击操作
        table.on('tool(category-list)',function (obj) {
            //layer.msg('edit');
            if(obj.event == 'edit'){

                //向新页面传递数据
                sessionStorage.setItem('category-edit',JSON.stringify(obj.data));
                //弹出层
                layer.open({
                    type: 2,
                    title: '编辑分类',
                    icon: '2',
                    area: ['800px','600px'],
                    content: '/category/edit.html',  //页面的url
                    end: function (){
                        //弹出层关闭，刷新表格
                        table.reload('category-list');
                    }
                });
            }else if(obj.event == 'del'){
                //layer.msg('delete');
                layer.confirm('确定要删除当前记录吗？',function (index){
                    layer.close(index);
                    //点击确定时操作
                    //ajax  /api/category   delete, 普通参数传ID
                    $.ajax({
                        url: '/api/category',
                        data: {id: obj.data.id},
                        type: 'delete',
                        dataType: 'JSON',
                        success: function (result){
                            if(result.code == 0){
                                layer.msg('删除成功');
                            }else{
                                layer.msg(result.msg);
                            }
                            //刷新表格
                            table.reload('category-list');
                        },
                        error: function (){
                            layer.msg('请求失败');
                        }
                    });
                });
            }
        })
    });
</script>